package culton.android.online.movie;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import culton.android.DetailsActivity;
import culton.android.R;
import culton.android.clicklistener.OnItemListClickListener;
import culton.android.entity.Movie;

public class MovieArrayAdapter extends ArrayAdapter<Movie> {
    private ArrayList<Movie> items;
    private Context mContext;
    private Intent extraInfo;
    
    public MovieArrayAdapter(Context context, int textViewResourceId, ArrayList<Movie> items, Intent extraInfo) {
        super(context, textViewResourceId, items);
        this.items = items;
        this.mContext = context;
        this.extraInfo = extraInfo;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    	View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.movies, null);
        }
        
        Movie movie = items.get(position);
        if (movie != null) {
            TextView tt = (TextView) v.findViewById(R.id.toptext);
            if (tt != null)
                  tt.setText(movie.getName());
        }

        Intent mIntent = null;
        String parentActivity = null;
        boolean isOnline = false;
        
        if (extraInfo != null) {
	        Bundle mBundle = extraInfo.getExtras();
	        isOnline = mBundle.getBoolean("online");
	        parentActivity = mBundle.getString("parent");
        }
        
        if (parentActivity == "moviesActivity") {
        	mIntent = new Intent(mContext, MovieCinemasActivity.class);
        	mIntent.putExtra("movieID", movie.getID());
        	mIntent.putExtra("online", isOnline);
        
        } else if (parentActivity == "NextMoviesActivity") {
        	mIntent = new Intent(mContext, MovieDetailsActivity.class);
        	mIntent.putExtra("movieID", movie.getID());
        	mIntent.putExtra("poster", movie.getPoster());
        	mIntent.putExtra("movieName", movie.getName());
        	mIntent.putExtra("synopsis", movie.getSynopsis());
        	mIntent.putExtra("genre", movie.getGenre());
        	mIntent.putExtra("duration", movie.getDuration());
        	mIntent.putExtra("rating", movie.getRating());
        	mIntent.putExtra("online", isOnline);
        	
        } else {
	        Bundle mBundle = extraInfo.getExtras();
	        mIntent = new Intent(mContext, DetailsActivity.class);
            mIntent.putExtra("cinemaID", mBundle.getString("cinemaID"));
            mIntent.putExtra("movieID", movie.getID());
            mIntent.putExtra("online", isOnline);
        }      

        if (movie.getID() != "none")
        	v.setOnClickListener(new OnItemListClickListener(mContext, mIntent));
        
        return v;
    }    
}